﻿
//-------------------------------------------------------------------------------
// File Name:	crc16.cpp
// Brief: 
// Version:		1.0.0
// Create Date: 2017/05/08
// Create by: 	Marshal Lee
// Copyright: 
//				Copyright (c)  2017, Richpeace Co., LTD.
//				All rights reserved.
//
// Modify by:	Marshal Lee
// Modify Date:	2017/05/08
//-------------------------------------------------------------------------------


#include "crc16.h"

//-------------------------------------------------------------------------------

#ifdef USE_CRC_TABLE

// 16 位 crc 列表
u16 CRC16_LIST[256] = 
{
	0x0000, 0x3096, 0x612C, 0x51BA, 0xC419, 0xF48F, 0xA535, 0x95A3, 
	0x8832, 0xB8A4, 0xE91E, 0xD988, 0x4C2B, 0x7CBD, 0x2D07, 0x1D91, 
	0x1064, 0x20F2, 0x7148, 0x41DE, 0xD47D, 0xE4EB, 0xB551, 0x85C7, 
	0x9856, 0xA8C0, 0xF97A, 0xC9EC, 0x5C4F, 0x6CD9, 0x3D63, 0x0DF5, 
	0x20C8, 0x105E, 0x41E4, 0x7172, 0xE4D1, 0xD447, 0x85FD, 0xB56B, 
	0xA8FA, 0x986C, 0xC9D6, 0xF940, 0x6CE3, 0x5C75, 0x0DCF, 0x3D59, 
	0x30AC, 0x003A, 0x5180, 0x6116, 0xF4B5, 0xC423, 0x9599, 0xA50F, 
	0xB89E, 0x8808, 0xD9B2, 0xE924, 0x7C87, 0x4C11, 0x1DAB, 0x2D3D, 
	0x4190, 0x7106, 0x20BC, 0x102A, 0x8589, 0xB51F, 0xE4A5, 0xD433, 
	0xC9A2, 0xF934, 0xA88E, 0x9818, 0x0DBB, 0x3D2D, 0x6C97, 0x5C01, 
	0x51F4, 0x6162, 0x30D8, 0x004E, 0x95ED, 0xA57B, 0xF4C1, 0xC457, 
	0xD9C6, 0xE950, 0xB8EA, 0x887C, 0x1DDF, 0x2D49, 0x7CF3, 0x4C65, 
	0x6158, 0x51CE, 0x0074, 0x30E2, 0xA541, 0x95D7, 0xC46D, 0xF4FB, 
	0xE96A, 0xD9FC, 0x8846, 0xB8D0, 0x2D73, 0x1DE5, 0x4C5F, 0x7CC9, 
	0x713C, 0x41AA, 0x1010, 0x2086, 0xB525, 0x85B3, 0xD409, 0xE49F, 
	0xF90E, 0xC998, 0x9822, 0xA8B4, 0x3D17, 0x0D81, 0x5C3B, 0x6CAD, 
	0x8320, 0xB3B6, 0xE20C, 0xD29A, 0x4739, 0x77AF, 0x2615, 0x1683, 
	0x0B12, 0x3B84, 0x6A3E, 0x5AA8, 0xCF0B, 0xFF9D, 0xAE27, 0x9EB1, 
	0x9344, 0xA3D2, 0xF268, 0xC2FE, 0x575D, 0x67CB, 0x3671, 0x06E7, 
	0x1B76, 0x2BE0, 0x7A5A, 0x4ACC, 0xDF6F, 0xEFF9, 0xBE43, 0x8ED5, 
	0xA3E8, 0x937E, 0xC2C4, 0xF252, 0x67F1, 0x5767, 0x06DD, 0x364B, 
	0x2BDA, 0x1B4C, 0x4AF6, 0x7A60, 0xEFC3, 0xDF55, 0x8EEF, 0xBE79, 
	0xB38C, 0x831A, 0xD2A0, 0xE236, 0x7795, 0x4703, 0x16B9, 0x262F, 
	0x3BBE, 0x0B28, 0x5A92, 0x6A04, 0xFFA7, 0xCF31, 0x9E8B, 0xAE1D, 
	0xC2B0, 0xF226, 0xA39C, 0x930A, 0x06A9, 0x363F, 0x6785, 0x5713, 
	0x4A82, 0x7A14, 0x2BAE, 0x1B38, 0x8E9B, 0xBE0D, 0xEFB7, 0xDF21, 
	0xD2D4, 0xE242, 0xB3F8, 0x836E, 0x16CD, 0x265B, 0x77E1, 0x4777, 
	0x5AE6, 0x6A70, 0x3BCA, 0x0B5C, 0x9EFF, 0xAE69, 0xFFD3, 0xCF45, 
	0xE278, 0xD2EE, 0x8354, 0xB3C2, 0x2661, 0x16F7, 0x474D, 0x77DB, 
	0x6A4A, 0x5ADC, 0x0B66, 0x3BF0, 0xAE53, 0x9EC5, 0xCF7F, 0xFFE9, 
	0xF21C, 0xC28A, 0x9330, 0xA3A6, 0x3605, 0x0693, 0x5729, 0x67BF, 
	0x7A2E, 0x4AB8, 0x1B02, 0x2B94, 0xBE37, 0x8EA1, 0xDF1B, 0xEF8D
};

#else

#ifdef SHIFT_RIGHT		// 如果是右移

#define CRC_GEN		0xa001	// 使用多项式 0xa001

#else					// 如果是左移

#define CRC_GEN		0x8005	// 使用多项式 0x8005

#endif	// #ifdef SHIFT_RIGHT

#endif

//-------------------------------------------------------------------------------

u16 addCrc16(u16 crcword, u8 dat)
{
#ifdef USE_CRC_TABLE	// 查表法crc
	crcword = (crcword >> 8) ^ CRC16_LIST[(crcword ^ dat) & 0xFF];
#else
	u8 i;
	crcword ^= dat;
	for (i = 0; i < 8; i++)
	{
#ifdef SHIFT_RIGHT			// 右移计算crc
		if ((LOBYTE(crcword) & 0x01) != 0)
		{
			crcword >>= 1;
			crcword ^= CRC_GEN;
		}
		else
		{
			crcword >>= 1;
		}
#else						// 左移计算crc
		if ((HIBYTE(crcword) & 0x80) != 0)
		{
			crcword <<= 1;
			crcword ^= CRC_GEN;
		}
		else
		{
			crcword <<= 1;
		}
#endif
	}

#endif
	return crcword;
}

//-------------------------------------------------------------------------------

u16 calcCrc16(u8 * pBuf, int lenBuf)
{
	int i;
	u16 crcword = CRC_INIT;

	for (i = 0; i < lenBuf; i++)
	{
        crcword = addCrc16(crcword, pBuf[i]);
	}

	return crcword;
}

//-------------------------------------------------------------------------------

u8 calcCheckSum8(u8 * pBuf, int lenBuf)
{
	int i;
	u8 checksum = 0;

	for (i = 0; i < lenBuf; i++)
	{
		checksum += pBuf[i];
	}

	return checksum;

}

//-------------------------------------------------------------------------------

u32 calcCheckSum32(u8 * pBuf, int lenBuf)
{
    int i;
    u32 checksum = 0;

    for (i = 0; i < lenBuf; i++)
    {
        checksum += pBuf[i];
    }

    return checksum;

}


